/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumjdbc;

import java.sql.Array;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class provides the primitives related to the <tt>java.sql.Array</tt> class.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
@PrimitiveProvider
public final class Arrays {

    /**
     * No instance of this class.
     */
    private Arrays() {
    } // end empty constructor

    /**
     * Frees the passed array.
     * @param ctxt context
     * @param array array
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_array_free(final CodeRunner ctxt,
                                               final Value array)
        throws Fail.Exception {
        try {
            final Array a = (Array) array.asBlock().asCustom();
            a.free();
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_array_free(CodeRunner, Value)'

    /**
     * Returns the passed array as a Java array.
     * @param ctxt context
     * @param array array
     * @return the passed array as a Java array
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_array_get_array(final CodeRunner ctxt,
                                                    final Value array)
        throws Fail.Exception {
        try {
            final Array a = (Array) array.asBlock().asCustom();
            return Cadmium.createObject(a.getArray());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_array_get_array(CodeRunner, Value)'

    /**
     * Returns a slice the passed array as a Java array.
     * @param ctxt context
     * @param array array
     * @param index starting index of the slice
     * @param count size of the slice
     * @return the passed array as a Java array
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_array_get_array_slice(final CodeRunner ctxt,
                                                          final Value array,
                                                          final Value index,
                                                          final Value count)
        throws Fail.Exception {
        try {
            final Array a = (Array) array.asBlock().asCustom();
            return Cadmium.createObject(a.getArray(index.asBlock().asInt64(),
                                                   count.asBlock().asInt32()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_array_get_array_slice(CodeRunner, Value, Value, Value)'

    /**
     * Returns the JDBC type of the elements of the passed array.
     * @param ctxt context
     * @param array array
     * @return the JDBC type of the elements of the passed array
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_array_get_base_type(final CodeRunner ctxt,
                                                        final Value array)
        throws Fail.Exception {
        try {
            final Array a = (Array) array.asBlock().asCustom();
            return Misc.encodeJDBCType(a.getBaseType());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_array_get_base_type(CodeRunner, Value)'

    /**
     * Returns the SQL type name of the elements of the passed array.
     * @param ctxt context
     * @param array array
     * @return the SQL type name of the elements of the passed array
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_array_get_base_type_name(final CodeRunner ctxt,
                                                             final Value array)
        throws Fail.Exception {
        try {
            final Array a = (Array) array.asBlock().asCustom();
            final Block res = Block.createString(a.getBaseTypeName());
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_array_get_base_type_name(CodeRunner, Value)'

} // end class 'Arrays'
